import asyncio
import json

from apps.database import mongo, get_ods, get_ods_result_by_id
from apps.global_config import configs
from apps.models.QueryModel import QueryDetail, QuerySchema


async def run_dwd():
    mongo.init(configs.MONGO_URI)
    qd = QueryDetail(field_name='status', field_value=2, link='and', oparation='eq')
    qd1 = QueryDetail(field_name='source_type', field_value=1, link='and', oparation='eq')
    # {"status": 1}
    query = QuerySchema(
        query=[qd, qd1],
        projection=['lngid']
    )
    # print(query.get_filter())
    result = await get_ods(query.get_filter(),
                           query.get_projection(),
                           query.get_sort_by(),
                           query.skip,
                           query.limit)
    with open('person.json','w',encoding='utf-8') as f:
        for one_obs in result:
            result_dicts = await get_ods_result_by_id(one_obs["lngid"])
            cclp_ods = result_dicts["data"]
            print(type(cclp_ods))
            line = json.dumps(cclp_ods,ensure_ascii=False)
            f.write(line+"\n")

if __name__ == "__main__":
    asyncio.run(run_dwd())
